/*
 * Copyright (c) 2012,东方口岸科技有限公司
 * All rights reserved.
 * 文件名称：RouterCalStarter.java
 * 摘    要：
 * 版本：1.0
 * 作    者：fengzhiyong
 */
package com.cneport.transfer.router.biz;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cneport.transfer.constant.RouterTypeConstants;
import com.cneport.transfer.router.api.IRouterMessageCalculator;
import com.cneport.transfer.router.api.ISendMessageCreater;
import com.cneport.transfer.router.model.NodeRelation;
import com.cneport.transfer.router.model.Router;
import com.cneport.transfer.router.model.SendMessage;
import com.cneport.transfer.router.support.service.NodeRelationService;
import com.cneport.transfer.router.support.service.RouterService;
import com.cneport.transfer.router.support.service.SendMessageService;

/**
 *@CreateDate:2012-7-11
 *@Description:路由下发任务
 */
@Component
public class RouterMessageManager {

    private static final Log logger = LogFactory
            .getLog(RouterMessageManager.class);

    private IRouterMessageCalculator routeCalculator;

    private ISendMessageCreater<Router> sendMessageCreater;
    @Autowired
    private RouterService routerService;
    @Autowired
    private SendMessageService sendMessageService;
    @Autowired
    private NodeRelationService nodeRelationService;


    public ISendMessageCreater<Router> getSendMessageCreater() {
        return sendMessageCreater;
    }

    public void setSendMessageCreater(ISendMessageCreater<Router> sendMessageCreater) {
        this.sendMessageCreater = sendMessageCreater;
    }

    public IRouterMessageCalculator getRouteCalculator() {
        return routeCalculator;
    }

    public void setRouteCalculator(IRouterMessageCalculator routeCalculator) {
        this.routeCalculator = routeCalculator;
    }

    /**
     * @Name: start 　* @Descrption:路由下发入口
     * @Author: fengzhiyong
     * @Version: V1.0
     * @CreateDate: 2012-7-11
     */
    public void start() {
        /*
         * 节点关联查询
         */
        List<NodeRelation> nodeRelationList = nodeRelationService
                .selectNodeRelation();
        if (nodeRelationList == null || nodeRelationList.size() == 0) {
            logger.error("没有节点互联信息，无法进行路由计算！");
            return;
        }
        /*
         * 路由计算
         */
        List<Router> routerList = routeCalculator.calculateRouteInfo(
                nodeRelationList, null);
        if (routerList == null || routerList.size() == 0) {
            logger.error("计算路由失败，未得到任何路由结果。");
        }
        /*
         * 路由入库
         */
        updateRouterInfo(routerList);
        /*
         * 待发送报文生成
         */
        List<SendMessage> sendMsgList = sendMessageCreater
                .sendMessageCreator(routerList);

        /*
         * 报文入待发送报文表
         */
        int addSendMessageNum =sendMessageService.insertSendMessage(sendMsgList);
        if (sendMsgList.size() == addSendMessageNum) {
            logger.info("待发送报文入库成功！添加待发送报文记录条数为：" + addSendMessageNum);
        } else {
            logger.error("待发送报文入库失败！添加待发送报文记录条数与应加待发送报文记录条数不服");
            return;
        }
    }
    public void updateRouterInfo(List<Router> routerList){
        routerService.deleteRouter(RouterTypeConstants.DTF_ROUTE);
        int addRouterNum = routerService.insertRouter(routerList);
        if (routerList.size() == addRouterNum) {
            logger.info("计算路由成功！添加路由记录条数为：" + addRouterNum);
        } else {
            logger.error("计算路由失败！添加路由记录条数与应加路由记录条数不服");
            return;
        }
    }

}
